import { Promise } from 'drunk-base';
import { getJsonWithToken, postJsonWithToken, putJsonWithToken, deleteJsonWithToken } from '../../../service/HttpClient';
import { HOST } from '../../../service/Config';
import { ITopicModel } from './TopicModel';
import { IPageMetaData } from '../../ListModel';

export function getTopic(id: string): Promise<ITopicModel> {
    return getJsonWithToken(`${HOST}/content/standard/topics/${id}`);
}

export function getTopicList(page = 0, size = 20): Promise<{ data: ITopicModel[], meta: IPageMetaData }> {
    return getJsonWithToken(`${HOST}/content/standard/topics?page=${page}&size=${size}`);
}

export function createTopic(data: ITopicModel): Promise<ITopicModel> {
    return postJsonWithToken(`${HOST}/content/standard/topics`, data);
}

export function updateTopic(id: string, data: ITopicModel): Promise<ITopicModel> {
    return putJsonWithToken(`${HOST}/content/standard/topics/${id}`, data);
}

export function deleteTopic(id: string) {
    return deleteJsonWithToken(`${HOST}/content/standard/topics/${id}`);
}

export function getTopicOptionsByName(name: string) {
    return getJsonWithToken(`${HOST}/content/standard/topics?name=${name}&page=0&size=100`)
        .then((res: { data: ITopicModel[], meta: IPageMetaData }) =>
            res.data.map(item => ({ id: item.id, name: item.name }))
        );
}

export function search(name: string, startTime: number, endTime: number, page = 0, size = 20): Promise<{ data: ITopicModel[], meta: IPageMetaData }> {
    return getJsonWithToken(`${HOST}/content/standard/topics?name=${name}&startTime=${startTime}&endTime=${endTime}&page=${page}&size=${size}`);
}
